package com.logica.mdm.webservices.messages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.logica.mdm.webservices.dao.beans.PartyByPhoneNumberResultBean;
//import com.logica.mdm.webservices.messages.beans.ContractBean;
//import com.logica.mdm.webservices.messages.beans.PartyBean;
import com.logica.mdm.webservices.util.RequestWithParamsBean;

/**
 * Utility class to search MDM by Phone Number.
 */
public class SearchPartyByPhoneNumberUtils {

	/**
	 * Create a query with params from the search parameter.
	 * 
	 * @param searchParams
	 *            the search parameter
	 * @return the corresponding query with params
	 */
	public static RequestWithParamsBean buildMdmIdRequest(
			SearchPartyByPhoneNumberRequest searchParams) {
		Map<String, Object> filterParams = new HashMap<String, Object>();

		// Fill request params
		String phoneNumber = searchParams.getPhoneNumber().trim();
		filterParams.put("homePhone", phoneNumber);
		filterParams.put("cellPhone", phoneNumber);
		filterParams.put("workPhone", phoneNumber);
		filterParams.put("faxPhone", phoneNumber);

		// Create the request
		RequestWithParamsBean request = new RequestWithParamsBean();
		request.setSqlQuery(SearchPartyByPhoneNumberConstants.PARTY_ID_SEARCH_QUERY);
		request.setParams(filterParams);
		return request;
	}

	/**
	 * Create a query with params from the search parameter.
	 * 
	 * @param searchParams
	 *            the search parameter
	 * @return the corresponding query with params
	 */
	public static RequestWithParamsBean buildMdmIdRequest(
			SearchPartyUnlimitedByPhoneNumberRequest searchParams) {
		Map<String, Object> filterParams = new HashMap<String, Object>();

		// Fill request params
		String phoneNumber = searchParams.getPhoneNumber().trim();
		filterParams.put("homePhone", phoneNumber);
		filterParams.put("cellPhone", phoneNumber);
		filterParams.put("workPhone", phoneNumber);
		filterParams.put("faxPhone", phoneNumber);

		// Create the request
		RequestWithParamsBean request = new RequestWithParamsBean();
		request.setSqlQuery(SearchPartyByPhoneNumberConstants.PARTY_ID_SEARCH_QUERY);
		request.setParams(filterParams);
		return request;
	}

	/**
	 * Create a query with params from the search parameter.
	 * 
	 * @param searchParams
	 *            the search parameter
	 * @return the corresponding query with params
	 */
	public static RequestWithParamsBean buildPartyRequest(
			List<PartyByPhoneNumberResultBean> partyByPhoneNumberResultBeans,
			Integer recordsToReturn) {
		List<String> partyFilterCriteria = new ArrayList<String>();
		Map<String, Object> filterParams = new HashMap<String, Object>();

		// Fill filter criteria and params
		partyFilterCriteria.add("c_party.rowid_object IN (:rowidObject)");
		List<String> partyIds = new ArrayList<String>(
				partyByPhoneNumberResultBeans.size());
		for (PartyByPhoneNumberResultBean party : partyByPhoneNumberResultBeans) {
			partyIds.add(party.getPartyId());
		}
		filterParams.put("rowidObject", partyIds);
		if (recordsToReturn != null) {
			filterParams.put("maxResult", recordsToReturn);
		}

		// Build the SQL query
		StringBuilder sb = new StringBuilder(4700);
		if (recordsToReturn != null) {
			sb.append(SearchPartyConstants.PARTY_START_PAGINATE);
		}
		sb.append(SearchPartyConstants.PARTY_SELECT_QUERY);
		sb.append(SearchPartyConstants.PARTY_INNER_QUERY_1);
		sb.append(StringUtils.join(partyFilterCriteria.toArray(), " AND "));
		sb.append(SearchPartyConstants.PARTY_INNER_QUERY_2);
		sb.append(SearchPartyConstants.PARTY_JOIN_QUERY);
		sb.append(SearchPartyConstants.PARTY_FINAL_FROM_NAME);
		sb.append(SearchPartyConstants.PARTY_ORDER_BY);

		if (recordsToReturn != null) {
			sb.append(SearchPartyConstants.PARTY_END_PAGINATE);
		}

		RequestWithParamsBean request = new RequestWithParamsBean();
		request.setSqlQuery(sb.toString());
		request.setParams(filterParams);
		return request;
	}

	/**
	 * Validate the input request. The request needs a Phone Number.
	 * 
	 * @param searchParams
	 *            the search parameter
	 * @return <null> if the request is valid, a string with an error message if
	 *         not
	 */
	public static String validateRequest(
			SearchPartyByPhoneNumberRequest searchParams) {
		// The result String
		String result = null;

		String searchLocation = searchParams.getSearchLocation();

		if (StringUtils.isEmpty(searchLocation)) {
			result = "Invalid input: you must specify a search location";
		} else if (!SearchPartyConstants.NEXIBOOK_SRC.equals(searchLocation)
				&& !SearchPartyConstants.SIGEO_SRC.equals(searchLocation)
				&& !SearchPartyConstants.HERMES_SRC.equals(searchLocation)) {
			result = "Invalid input: invalid search location";
		}

		if (StringUtils.isEmpty(searchParams.getPhoneNumber())) {
			result = "Invalid input: you must specify a phone number";
		}

		return result;
	}

	/**
	 * Validate the input request. The request needs a Phone Number.
	 * 
	 * @param searchParams
	 *            the search parameter
	 * @return <null> if the request is valid, a string with an error message if
	 *         not
	 */
	public static String validateRequest(
			SearchPartyUnlimitedByPhoneNumberRequest searchParams) {
		// The result String
		String result = null;

		String searchLocation = searchParams.getSearchLocation();

		if (StringUtils.isEmpty(searchLocation)) {
			result = "Invalid input: you must specify a search location";
		} else if (!SearchPartyConstants.CTI_SRC.equals(searchLocation)) {
			result = "Invalid input: invalid search location";
		}

		if (StringUtils.isEmpty(searchParams.getPhoneNumber())) {
			result = "Invalid input: you must specify a phone number";
		}

		return result;
	}

	/**
	 * Regularize the response by aggregating contract information of a entity
	 * (ID MDM).
	 * 
	 * @param searchPartyResponse
	 *            the search party response
	 * @return the regularized searchPartyResponse
	 */
	/*public static SearchPartyByPhoneNumberResponse regularizeOutput(
			SearchPartyByPhoneNumberResponse searchPartyResponse) {
		List<PartyBean> arraylistOfPartyBean = new ArrayList<PartyBean>();
		for (PartyBean oPartyBean : searchPartyResponse.getPartyResults()) {
			boolean aggregated = false;
			for (PartyBean partyBean : arraylistOfPartyBean) {
				if (StringUtils.equals(partyBean.getMdmId(),
						oPartyBean.getMdmId())) {
					List<ContractBean> arraylistOfContractBean = _regulariseOutput(
							oPartyBean).getContract();
					if (arraylistOfContractBean.size() > 0) {
						// partyBean.getContract().add(arraylistOfContractBean.get(0));
						for (ContractBean contractBean : arraylistOfContractBean) {
							partyBean.getContract().add(contractBean);
						}
					}
					aggregated = true;
					break;
				}
			}
			if (!aggregated) {
				arraylistOfPartyBean.add(_regulariseOutput(oPartyBean));
			}
		}
		searchPartyResponse.setPartyResults(arraylistOfPartyBean);
		return searchPartyResponse;
	}*/

	/**
	 * Format a given PartyBean (extract the contract information overview and
	 * fill a ContractBean which is added to the returned PartyBean)
	 * 
	 * @param PartBean
	 *            the PartyBean to regularize
	 * @return the regularized PartyBean
	 */
	/*private static PartyBean _regulariseOutput(PartyBean partyBean) {
		ContractBean contractBean = new ContractBean();
		if (!StringUtils.isEmpty(partyBean.getContractType())) {
			contractBean.setContractType(partyBean.getContractType());
			partyBean.setContractType(null);
		}
		if (!StringUtils.isEmpty(partyBean.getContractStatus())) {
			contractBean.setContractStatus(partyBean.getContractStatus());
			partyBean.setContractStatus(null);
		}
		if (!StringUtils.isEmpty(partyBean.getContractValidityStartDate())) {
			contractBean.setContractValidityStartDate(partyBean
					.getContractValidityStartDate());
			partyBean.setContractValidityStartDate(null);
		}
		if (!StringUtils.isEmpty(partyBean.getContractValidityEndDate())) {
			contractBean.setContractValidityEndDate(partyBean
					.getContractValidityEndDate());
			partyBean.setContractValidityEndDate(null);
		}
		ArrayList<ContractBean> arraylistOfContractBean = new ArrayList<ContractBean>();
		arraylistOfContractBean.add(contractBean);
		partyBean.setContract(arraylistOfContractBean);
		return partyBean;
	}*/
}
